// import { BrowserRouter,Route,Routes } from "react-router-dom";
// import Login from '@/pages/Login/Login'

// function Routers(){
//     return (
//         <BrowserRouter>
//             <Routes>
//                 <Route path='/login' element={<Login></Login>}></Route>
//             </Routes>
//         </BrowserRouter>
//     )
// }

// export default Routers


import React, { Suspense } from "react";

const Layout = React.lazy(() => import('@/Layouts'))
const Login = React.lazy(() => import('@/pages/Login/Login'))

// 二级路由
const Home = React.lazy(() => import('@/pages/Home'))
const Profile = React.lazy(() => import('@/pages/Profile'))
const Question = React.lazy(() => import('@/pages/Question'))
const Video = React.lazy(() => import('@/pages/Video'))
const Article = React.lazy(() => import('@/pages/Article'))
// const Search = React.lazy(() => import('@/pages/Search'))

const NotFound = React.lazy(() => import('@/pages/NotFound'))


// 上面的组件是通过懒加载进来的 所以渲染页面可能会有延迟 使用Suspense 优化交互
// fallback 加载完成前做的事情
const lazyLoad = (children) => {
    return <Suspense fallback={<>loading</>}>{children}</Suspense>
}

const router = [
    {
        path: '/',
        element: <Layout />,
        auth: true, // 判断是否需要鉴权
        children: [
            {
                index: true,
                element: lazyLoad(<Home />),
                auth: false
            },
            {
                path: '/profile',
                element: lazyLoad(<Profile />),
                auth: true
            },
            {
                path: '/question',
                element: lazyLoad(<Question />),
                auth: false
            },
            {
                path: '/video',
                element: lazyLoad(<Video />),
                auth: false
            },

        ],
    },
    {
        path: '/article/:id',
        element: lazyLoad(<Article />),
    },
    // {
    //     path: '/search',
    //     element: lazyLoad(<Search />),
    // },
    {
        path: '/login',
        element: lazyLoad(<Login />),
        auth: false,
    },
    {
        path: '*',
        element: <NotFound />,
        auth: false,
      },
]

// 根据浏览器地址栏的路径获取路由
/* 
router 是所有的路由对象
   path 当前的路由地址
*/

const checkAuth = (router,path) =>{
    for(const data of router){
        if(data.path === path) return data
        if(data.children){
            const res = checkAuth(data.children,path)
            if(res) return res
        }
    }
    return null
}

//简化上面的方法
const checkRouterAuth = (path) =>{
    // 存储查找到的路由对象
    let authObj = null
    // router 获取的是全局的路由对象  18行定义的
    authObj = checkAuth(router,path)
    return authObj
}

export {router, checkRouterAuth}